#include "../header/compiler.h"
#include "../header/stack.h"
#include <stdio.h>
#include <string.h>
int getSuffix(char c)
{
    switch(c)
    {
	case 'i':
	    return 0;
	case '=':
	    return 1;
	case '+':
	    return 2;
	case '*':
	    return 3;
	case '(':
	    return 4;
	case ')':
	    return 5;
	case ';':
	    return 6;
	case 'S':
	    return 0;
	case 'E':
	    return 1;
	case 'T':
	    return 2;
	case 'F':
	    return 3;
    }
}
char semantic(int s,SqStackchar* charStack)
{
    //s->产生式的序号
    static int i = 0;
    int j = 0;
    char m = 's';
    character ch;
    char buf[3];
    character p[MAX_PRO_SIZE];
    for(j = grammar[s].rightnum-1;j >= 0 ;j--)
    {
        p[j] = Popchar(charStack); 
    }
    ch.symbol = grammar[s].left;
    switch(s)
    {
        case 0:
            fprintf(fpout,"%s",p[0].value);
            fprintf(fpout,"=");
            fprintf(fpout,"%s\n",p[2].value);
            fprintf(fpout,"acc");
            break;
	case 1:
	    sprintf(buf, "%c%d", m, i++);
            buf[2] = '\0';
	    fprintf(fpout,"%s",buf);
	    fprintf(fpout,"%c",'=');
	    fprintf(fpout,"%s",p[0].value);
	    fprintf(fpout,"%c",'+');
	    fprintf(fpout,"%s",p[2].value);
	    fprintf(fpout,"\n");
            strcpy(ch.value,buf);
            Pushchar(charStack,ch);
            break;
	case 2:
	    strcpy(ch.value,p[0].value);
            Pushchar(charStack,ch);
	    break;
	case 3:
	    sprintf(buf, "%c%d", m, i++);
            buf[2] = '\0';
	    fprintf(fpout,"%s",buf);
	    fprintf(fpout,"%c",'=');
	    fprintf(fpout,"%s",p[0].value);
	    fprintf(fpout,"%c",'*');
	    fprintf(fpout,"%s",p[2].value);
	    fprintf(fpout,"\n");
            strcpy(ch.value,buf);
            Pushchar(charStack,ch);
            break;
	case 4:
	    strcpy(ch.value,p[0].value);
            Pushchar(charStack,ch);
	    break;
	case 5:
	    strcpy(ch.value,p[1].value);
	    Pushchar(charStack,ch);
            break;
	case 6:
	    strcpy(ch.value,p[0].value);
	    Pushchar(charStack,ch);
            break;
    }
    return  ch.symbol;
}
void syntaxAnalysis()
{ 
    int i;
    char c ,d ;
    int sign = 0;
    stateStack = Initstackint();
    charStack = Initstackchar();
    fpin = fopen("out.txt", "r");
    if(fpin == NULL)
    {
	printf("cannot open infile\n");
	return;
    }
    fpout = fopen("compile.o","w");
    Push(stateStack,0);
    
    while(c!=EOF)
    {
	int suffix;
	char idvalue[MAX_IDEN_LEN];
	int k = 0;
	if(sign == 0)
	{
	    fscanf(fpin, "%d", &i);
	    c = fgetc(fpin);
	    d = fgetc(fpin);
	    if( i == 50)
	    {
		do
		{
		    idvalue[k++] = d;
		    d = fgetc(fpin);
		}while(d!='\t');
		idvalue[k] = '\0';
	    }
	    else
	    {
		fgetc(fpin);
	    }

	    
	}
	sign = 0;
	if(i == 50)
	    suffix = getSuffix('i');
	else
	    suffix = getSuffix(d);
	int a = action[gettopint(stateStack)][suffix];
	if(a == 0)
	{
	    fprintf(fpout,"error\n");
	    fprintf(fpout,"sentence illegal!");
	    return ;
	}
	else if(a == 1)//做移入
	{
	    if(i == 50)
	    {
		character cha ;
		cha.symbol = 'i';
		strcpy(cha.value,idvalue );
		Pushchar(charStack,cha);
	    }
	    else
	    {
		character cha ;
		cha.symbol = d;
		strcpy(cha.value,"\0");
		Pushchar(charStack,cha);
	    }
		
	    int s = actionselect[gettopint(stateStack)][suffix];
	    Push(stateStack,s);
	}
	else if(a == 2)//归约
	{
	    int s = actionselect[gettopint(stateStack)][suffix];
	    int size = grammar[s].rightnum;
	    int j;
	    for(j = 0; j < size; j++)
	    {
		Popint(stateStack);
	    }
	    
	    char symbol = semantic(s,charStack);
	    suffix = getSuffix(symbol);
	    s = gotoState[gettopint(stateStack)][suffix];
	    Push(stateStack,s);
	    sign = 1;
	}
	else if(a == 3)
	{
            semantic(0,charStack);   
	    return;
	}
    }
}
